package com.study.redissession.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*********************************************************
 * @Author ONE_LLX
 * @Description //TODO 
 * @Date 16:38 2019/11/12
 * @Class HelloController
 * @Version v1.0
 *********************************************************/
@CrossOrigin(origins = "*", maxAge = 3600, methods = {RequestMethod.POST, RequestMethod.GET})
@RestController
@RequestMapping("/api/user")
public class HelloController {

    @RequestMapping(value = "/find/list", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String findList(HttpSession session) {
//        System.out.println(1/0);

        return session.getId();
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String account, String password)
    {
//        User user = userService.findUserByAccountAndPassword(account, password);
        if ( password.equals(account))
        {
            HttpSession session = request.getSession();
            session.setAttribute("loginUserId", account);
            redisTemplate.opsForValue().set("loginUser:" + account, session.getId());

            return session.getId();
        }
        else
        {
            return "account or password error！ please login again!";
        }
    }

}

